import Vue from 'vue'
import iView from 'iview'
import 'iview/dist/styles/iview.css'
// 主题覆盖
import 'common/theme/index.less'
import BootstrapVue from 'bootstrap-vue'
import App from './App'
import router from './router'
import VueRouter from 'vue-router'
import { post, get, put, _delete } from 'common/js/http'

// 判断页面未登录跳转到登录页面
router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requireAuth)) {
    if (localStorage.getItem('user_info')) {
      next()
    } else {
      next({
        path: from.fullPath,
        query: {redirect: to.fullPath} // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
    }
  } else {
    next()
  }
})

// 配置主域名
window.HOST = 'http://demo.vuetp.com'
// 全局对象
const vm = new Vue()
window.vm = vm

const routers = new VueRouter({
  mode: 'hash',
  router
})
window.router = routers

Vue.use(BootstrapVue)
Vue.use(iView)
Vue.use(VueRouter)
// post请求
Vue.prototype.$post = post
// get请求
Vue.prototype.$get = get
// put请求
Vue.prototype.$put = put
// put请求
Vue.prototype.$_delete = _delete

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  template: '<App/>',
  components: {
    App
  }
})

// 全局配置iview
window.vm.$Message.config({
  top: 100,
  duration: 1
})
window.vm.$Notice.config({
  top: 100,
  duration: 3
})
